﻿#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QSet>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //填充投票，并设置列表框显示
    FillDataAndListWidget();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButtonGetVotes_clicked()
{
    //投票人是 keys
    QList<QString> listVoters = m_voteData.keys();
    //用集合去重复
    QSet<QString> uniqueVoters( listVoters.begin(), listVoters.end() );
    //可以转回列表
    listVoters.assign(uniqueVoters.begin(), uniqueVoters.end());
    //构造信息字符串
    int nCount = listVoters.count();
    QString strInfo = tr("投票人数：%1 \r\n").arg(nCount);
    for(int i=0; i<nCount; i++)
    {
        strInfo += listVoters[i] + tr("\r\n");
    }
    //显示
    ui->textBrowser->setText( strInfo );
}


void MainWindow::on_pushButtonGetCandidates_clicked()
{
    //候选人是 values
    QList<QString> listCandidates = m_voteData.values();
    //用集合去重复
    QSet<QString> uniqueCandidates(listCandidates.begin(),  listCandidates.end());
    //转回列表
    listCandidates.assign(uniqueCandidates.begin(), uniqueCandidates.end());
    //构造信息字符串
    int nCount = listCandidates.count();
    QString strInfo = tr("候选人数：%1 \r\n").arg(nCount);
    for(int i=0; i<nCount; i++)
    {
        strInfo += listCandidates[i] + tr("\r\n");
    }
    //显示
    ui->textBrowser->setText( strInfo );
}


void MainWindow::on_pushButtonCalcBoth_clicked()
{
    QSet<QString> voteZhangSan;
    QSet<QString> voteGouGe;
    //使用迭代器遍历
    QMultiHash<QString, QString>::const_iterator it = m_voteData.constBegin();
    while ( it != m_voteData.constEnd() )
    {
        if( it.value() == tr("张三") )
        {
            voteZhangSan<<it.key();
        }
        if( it.value() == tr("狗哥") )
        {
            voteGouGe<<it.key();
        }
        ++it;
    }
    //张三的投票人集合，与狗哥的投票人集合 求交集
    QSet<QString> sIntersect = voteZhangSan & voteGouGe;
    QStringList listIntersect(sIntersect.begin(), sIntersect.end());
    //构造信息字符串
    QString strInfo = tr("同时选择张三和狗哥的投票人数：%1 \r\n").arg(listIntersect.count());
    strInfo += listIntersect.join( "\r\n" );
    //显示
    ui->textBrowser->setText( strInfo );
}


void MainWindow::on_pushButtonCalcOr_clicked()
{
    QSet<QString> voteZhangSan;
    QSet<QString> voteGouGe;
    //使用迭代器遍历
    QMultiHash<QString, QString>::const_iterator it = m_voteData.constBegin();
    while ( it != m_voteData.constEnd() )
    {
        if( it.value() == tr("张三") )
        {
            voteZhangSan<<it.key();
        }
        if( it.value() == tr("狗哥") )
        {
            voteGouGe<<it.key();
        }
        ++it;
    }
    //张三的投票人集合，与狗哥的投票人集合 求交集
    QSet<QString> sIntersect = voteZhangSan | voteGouGe;
    QStringList listIntersect(sIntersect.begin(), sIntersect.end());
    //构造信息字符串
    QString strInfo = tr("选择张三或狗哥的投票人数：%1 \r\n").arg(listIntersect.count());
    strInfo += listIntersect.join( "\r\n" );
    //显示
    ui->textBrowser->setText( strInfo );
}

void MainWindow::FillDataAndListWidget(void)
{
    m_voteData.insert( tr("路人甲"), tr("张三") );
    m_voteData.insert( tr("路人甲"), tr("狗哥") );
    m_voteData.insert( tr("路人乙"), tr("李四") );
    m_voteData.insert( tr("路人乙"), tr("狗哥") );
    m_voteData.insert( tr("路人丙"), tr("张三") );
    m_voteData.insert( tr("路人丙"), tr("狗哥") );
    m_voteData.insert( tr("路人丁"), tr("李四") );
    m_voteData.insert( tr("路人丁"), tr("狗哥") );
    m_voteData.insert( tr("路人戊"), tr("张三") );
    m_voteData.insert( tr("路人戊"), tr("狗哥") );
    //显示到列表控件
    ui->listWidget->clear();
    //使用迭代器遍历
    QMultiHash<QString, QString>::const_iterator it = m_voteData.constBegin();
    while ( it != m_voteData.constEnd() )
    {
        QString strLine = it.key() + tr("\t投\t") + it.value();
        ui->listWidget->addItem( strLine );
        ++it;
    }
}
